<?php

/**
* Controller:  "Schedule"
*
* This controller processes information received from the secure section, then
* adds the information as a new event in the database.  Once added, the event is
* viewable from the "Events" page on the front-end of the site.
*
* Functions:  index
*
* @author     Brandon A Johnson
* @copyright  LIS4368-01: "Web Development with PHP," Spring 2010
* @version    1.0
* @date       April 24, 2010
*
*/

class Schedule extends Controller {
	
	/**
	* Schedule() - constructor
	*
	* allows this file to act as a valid CodeIgniter controller
	*/
	function Schedule()
	{
		parent::Controller();
	}

	/**
	* index()
	*
	* gathers all event records, then takes the data the user just entered
	* and enters that information into the database as a new record.  Now,
	* this event will be listed with all the others
	*/
	function index()
	{
		// allows the form information to be validated
		$this->load->library('form_validation');

		// loads the "pets" database interaction model
		$this->load->model('pets_model');
		// extracts all pet records currently in the table
		$all_pets = $this->pets_model->list_pets();
		// prepares the data to be sent to the view
		$data = array();
		$data['results'] = $all_pets;
			
		// loads the "events" database interaction model
		$this->load->model('events_model');
		// extracts all event records currently in the table
		$all_events = $this->events_model->list_events();
		// prepares the data to be sent to the view
		$data['events'] = $all_events;

		// if the event has not been given a name...
		if($_POST['name'] == NULL)
		{
			// ...print an error message...
			echo "Sorry, but the event could not be added because it has no name!  Nobody can go to an event if they don't know what it is...";

			// ...and reload the page
			$this->load->view('header');
			$this->load->view('secure', $data);
		}
		// otherwise...
		else
		{
			// check to see if all required fields have been added
			$this->form_validation->set_rules('name', 'Name', 'trim|required');
			$this->form_validation->set_rules('sponsor', 'Sponsor', 'trim');
			$this->form_validation->set_rules('location', 'Location', 'trim');
			$this->form_validation->set_rules('month', 'Month', 'required');
			$this->form_validation->set_rules('day', 'Day', 'required');
			$this->form_validation->set_rules('year', 'Year', 'required');
			$this->form_validation->set_rules('time', 'Time', 'required');
			// validate the data
			$result = $this->form_validation->run();

			// prep the data to be sent to the database
			$name = $_POST['name'];
			$sponsor = $_POST['sponsor'];
			$location = $_POST['location'];
			/* special concatenation of year/month/day selections to match the database data type */
			$date = $_POST['year'] . "-" . $_POST['month'] . "-" . $_POST['day'];
			$time = $_POST['time'];
			$description = $_POST['description'];

			// if everything validated correctly...
			if ($result == TRUE)
			{
				// send the new record to the database
				$this->events_model->schedule_event($name, $sponsor, $location, $date, $time, $description);
				
				// clear the POST array, just in case
				$_POST = NULL;
		
				// print a success message
				echo "The " . $name . " at " . $location . " was successfully scheduled! Hope the weather holds out!";
			}
			// otherwise, print an error message
			else
			{
				echo "Something went wrong.  Please try again...";
			}
		}

		// reload the event records, including the new one
		$all_events = $this->events_model->list_events();
		$data['events'] = $all_events;
		
		// reload the page
		$this->load->view('header');
		$this->load->view('secure', $data);
	}
}

/* End of file schedule.php */
/* Location: ./system/application/controllers/schedule.php */